package com.lovo.pis.controller;

import com.lovo.pis.constant.MessageCode;
import com.lovo.pis.dto.EmailBean;
import com.lovo.pis.dto.UserBean;
import com.lovo.pis.utils.EmailUtil;
import com.lovo.pis.utils.JsonResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
public class EmailController {

    @RequestMapping("/sendEmail")
    public JsonResult sendEmail(@RequestBody EmailBean emailBean) {
        JsonResult jsonResult = new JsonResult();
        if (emailBean.getSubject() == null || emailBean.getSubject() == "" || emailBean.getContent() == null || emailBean.getContent() == "") {
            jsonResult.setCode(MessageCode.PARAM_ERROR.getCode());
            jsonResult.setMessage(MessageCode.PARAM_ERROR.getMessage());
            return jsonResult;
        }
        try {
            EmailUtil.sendEmail(emailBean.getReceiver(), emailBean.getSubject(), emailBean.getContent(), emailBean.getSender().getRealName());
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(MessageCode.ERROR.getCode());
            jsonResult.setMessage(MessageCode.ERROR.getMessage());
        }
        return jsonResult;
    }
}
